package org.example.spring.context;

import org.example.spring.factory.MyAnnotationBeanDefinitionReader;
import org.example.spring.factory.MyBeanFactory;
import org.example.spring.util.MyAssert;

public class MyAnnotationConfigApplicationContext extends MyGeneralApplicationContext {

    private final Object monitor = new Object();

    public MyAnnotationConfigApplicationContext(Class... annotationClass) {

        super();

        register(annotationClass);

        refresh();
    }

    public void register(Class... annotationClass) {
        MyAssert.noNullElements(annotationClass, "at leaset one annotated class must be specified");

        MyAnnotationBeanDefinitionReader reader = new MyAnnotationBeanDefinitionReader(getMyBeanFactory());
        reader.registerBeanDef(annotationClass);
    }

    public void refresh() {
        synchronized (this.monitor) {
            MyBeanFactory myBeanFactory = getMyBeanFactory();

            finishBeanFactoryInitialization(myBeanFactory);
        }
    }

    public void finishBeanFactoryInitialization(MyBeanFactory myBeanFactory) {
        myBeanFactory.preInstantiateSingletons();
    }

}
